﻿using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LibVLCSharp.Shared;
using LibVLCSharp.WPF;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using RestSharp; // 核心命名空间，包含 LibVLC 和 MediaPlayer 等关键类
using VideoClient;
using vlcplay.ViewModels; 
// using System.Reflection; // Removed duplicate
using MahApps.Metro.Controls;
namespace vlcplay;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{
    private readonly ILogger<MainWindow> logger = App.ServiceProvider.GetRequiredService<ILogger<MainWindow>>();

    private MainViewModel _viewModel;
    public MainWindow()
    {
        InitializeComponent();
        _viewModel = App.ServiceProvider.GetRequiredService<MainViewModel>();
        DataContext = _viewModel;
        this.Closing += MainWindow_Closing;


        // 日志输出版本号
    }

    private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e)
    {
        // 释放 ViewModel 中的所有资源
        // 先停止所有视频播放
        _viewModel?.StopAllVideos();
        // 再释放所有资源
        _viewModel?.Dispose();
        // 强制关闭窗口，防止残留
        //Application.Current.Shutdown();
    }



    private async void Window_LoadedAsync(object sender, RoutedEventArgs e)
    {
#if DEBUG
        // Debug模式自动登录
        if (DataContext is MainViewModel vm)
        {
            vm.CurrentUser = "leiyang";
            // 直接调用登录接口
            vm.Login("leiyang", "pwd");
            await vm.LoadVideoListAsync();
        }
#else
        // Release模式弹出登录窗口
        var xhVideoClient = App.ServiceProvider.GetRequiredService<XhVideoClient>();
        var loginWin = App.ServiceProvider.GetRequiredService<LoginWindow>();
        if (loginWin.ShowDialog() == true)
        {
            // 记住用户名
            if (loginWin.RememberMe)
            {
                UserStore.SaveUserName(loginWin.UserName);
            }
            else
            {
                UserStore.SaveUserName("");
            }

            if (DataContext is MainViewModel vm)
            {
                vm.CurrentUser = loginWin.UserName;
                await vm.LoadVideoListAsync();
            }
        }
        else
        {
            Close();
        }
#endif

    }




    protected override void OnPreviewKeyDown(KeyEventArgs e)
    {
        base.OnPreviewKeyDown(e);
    }

    private void Window_KeyDown(object sender, KeyEventArgs e)
    {
        // Esc key for maximized window is handled by MaximizedVideoWindow itself
    }


    // Handle the SizeChanged event of the ItemsControl to update the ViewModel
    private void ItemsControl_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        if (DataContext is MainViewModel vm)
        {
            // Update the AvailableItemsControlWidth property in the ViewModel
            vm.AvailableItemsControlWidth = e.NewSize.Width;
        }
        //Application.Current.Dispatcher.InvokeAsync(() =>
        //{
        //    // 假设 itemsControl 是你的 ItemsControl 名称
        //    itemsControl.InvalidateVisual();
        //    itemsControl.UpdateLayout();
        //});
    }

    // 移除了 VideoView_Loaded 事件处理器 (原用于ItemsControl中的VideoView)

    // 移除了 VideoView_Unloaded 事件处理器 (原用于ItemsControl中的VideoView)

    // 移除了 VideoViewMax_Loaded, VideoViewMax_Unloaded, MaximizedVideoView_IsVisibleChanged
    // 因为它们未在 XAML 中附加到 MaximizedVideoView 控件。
}
